Un rapport sur les erreurs de classification des races de chiens a été soumis à l’association de protection des animaux. Dans ce rapport, il est recommandé d’implémenter une solution de détection d’objet pour améliorer la qualité des photos. Cette solution doit isoler l’objet à classifier en procédant à un rognage. Cette nouvelle méthode fera appel au dernier YOLO, version 4.
Ce notebook présente la phase 1.
from google.colab import drive
drive.mount('/content/drive')
Mounted at /content/drive
import pandas as pd
import numpy as np
import os
import matplotlib.pyplot as plt
plt.style.use("default")
import seaborn as sns
from keras.models import load_model
from keras.preprocessing.image import load_img, img_to_array
from sklearn.metrics import classification_report
def get_breed(file):
image = load_img(file, target_size=(299, 299))
input_array = np.reshape(img_to_array(image), (-1, 299, 299, 3)) / 255
# Prédiction de la race
output = model.predict(input_array)
return class_label.at[output.argmax(axis=-1)[0], 'index']
def get_top3_breeds(model, file):
image = load_img(file, target_size=(299, 299))
input_array = np.reshape(img_to_array(image), (-1, 299, 299, 3)) / 255
# Prédiction de la race
output = model.predict(input_array)
df_out = pd.DataFrame(output[0])
top3_breeds = pd.merge(class_label, df_out, left_index=True, right_index=True).rename(columns={0: 'Probabilité', 'index': 'Race prédite'}).sort_values('Probabilité', ascending=False).head(3)
return top3_breeds
def get_true_breed(file):
return file.split('/')[-2].split('-')[1]
def get_best_object(df):
result_df = pd.DataFrame()
for i in range(len(df.objects)):
res = pd.DataFrame(df.objects[i], columns=['class_id', 'name', 'relative_coordinates', 'confidence'])
if res.empty:
# NaN si aucun objet détecté (la photo ne sera pas recadrée)
res.loc[0] = np.nan
else:
# Sélection du chien avec la meilleure confidence
res = res[res.name == "dog"].sort_values(by="confidence", ascending=False).head(1).reset_index(drop=True)
# NaN si aucun chien détecté (la photo ne sera pas recadrée)
if res.empty:
res.loc[0] = np.nan
else:
# Extraction des coordonnées de recadrage
coord = pd.DataFrame.from_dict(res.relative_coordinates.loc[0], orient='index').transpose()
res = pd.merge(res, coord, left_index=True, right_index=True)
# Ajout dans un df
result_df = result_df.append(res)
return result_df.reset_index(drop=True)
path = '/content/drive/My Drive/Formation/Informatique - Digital/OpenClassroom/IML/P7_Développez_une_preuve_de_concept/dev/src/'
model = load_model(path + 'best_model_Xception_DataAugmentation_120_breeds.hdf5')
class_label = pd.read_csv(path + 'class_labels.csv')
test_file = pd.read_csv(path + 'dog_test_split.csv')
print(f'Nous avons {test_file.shape[0]} images de test')
Nous avons 4116 images de test
path_img = '/content/drive/My Drive/Formation/Informatique - Digital/OpenClassroom/IML/P6_Classez_des_images_à_l_aide_d_algorithmes_de_deep_learning/dev/'
test_file.uri = test_file.uri.apply(lambda x : path_img + x).str.replace('\\', '/')
%time test_file["predicted_breeds"] = test_file.uri.apply(lambda x: get_breed(x))
test_file
CPU times: user 3min 23s, sys: 25.9 s, total: 3min 49s Wall time: 22min 28s
| uri | breeds | predicted_breeds | |
|---|---|---|---|
| 0 | /content/drive/My Drive/Formation/Informatique... | Boston_bull | Boston_bull |
| 1 | /content/drive/My Drive/Formation/Informatique... | toy_poodle | miniature_poodle |
| 2 | /content/drive/My Drive/Formation/Informatique... | Staffordshire_bullterrier | Staffordshire_bullterrier |
| 3 | /content/drive/My Drive/Formation/Informatique... | dingo | dingo |
| 4 | /content/drive/My Drive/Formation/Informatique... | West_Highland_white_terrier | West_Highland_white_terrier |
| ... | ... | ... | ... |
| 4111 | /content/drive/My Drive/Formation/Informatique... | Staffordshire_bullterrier | Staffordshire_bullterrier |
| 4112 | /content/drive/My Drive/Formation/Informatique... | Old_English_sheepdog | Old_English_sheepdog |
| 4113 | /content/drive/My Drive/Formation/Informatique... | Irish_terrier | Irish_terrier |
| 4114 | /content/drive/My Drive/Formation/Informatique... | Bedlington_terrier | Bedlington_terrier |
| 4115 | /content/drive/My Drive/Formation/Informatique... | clumber | clumber |
4116 rows × 3 columns
error_predict = test_file[test_file.breeds != test_file.predicted_breeds]
error_predict.reset_index(drop=True, inplace=True)
error_predict
| uri | breeds | predicted_breeds | |
|---|---|---|---|
| 0 | /content/drive/My Drive/Formation/Informatique... | toy_poodle | miniature_poodle |
| 1 | /content/drive/My Drive/Formation/Informatique... | standard_schnauzer | miniature_schnauzer |
| 2 | /content/drive/My Drive/Formation/Informatique... | Sealyham_terrier | soft-coated_wheaten_terrier |
| 3 | /content/drive/My Drive/Formation/Informatique... | Japanese_spaniel | Pekinese |
| 4 | /content/drive/My Drive/Formation/Informatique... | Yorkshire_terrier | silky_terrier |
| ... | ... | ... | ... |
| 512 | /content/drive/My Drive/Formation/Informatique... | cairn | Border_terrier |
| 513 | /content/drive/My Drive/Formation/Informatique... | Lhasa | Shih-Tzu |
| 514 | /content/drive/My Drive/Formation/Informatique... | English_springer | German_short-haired_pointer |
| 515 | /content/drive/My Drive/Formation/Informatique... | Australian_terrier | silky_terrier |
| 516 | /content/drive/My Drive/Formation/Informatique... | West_Highland_white_terrier | Maltese_dog |
517 rows × 3 columns
i = 1
for img in error_predict.uri.head(20):
print(f"Cas n°{i}")
display(load_img(img))
display("Race indiquée par les données : " + get_true_breed(img))
display(get_top3_breeds(model, img))
print("#############################################################")
print("")
i += 1
Cas n°1
'Race indiquée par les données : toy_poodle'
| Race prédite | Probabilité | |
|---|---|---|
| 104 | miniature_poodle | 0.652671 |
| 115 | toy_poodle | 0.347219 |
| 41 | Lakeland_terrier | 0.000037 |
############################################################# Cas n°2
'Race indiquée par les données : standard_schnauzer'
| Race prédite | Probabilité | |
|---|---|---|
| 105 | miniature_schnauzer | 8.540156e-01 |
| 114 | standard_schnauzer | 1.459842e-01 |
| 94 | giant_schnauzer | 1.734078e-07 |
############################################################# Cas n°3
'Race indiquée par les données : Sealyham_terrier'
| Race prédite | Probabilité | |
|---|---|---|
| 112 | soft-coated_wheaten_terrier | 0.956069 |
| 61 | Sealyham_terrier | 0.037118 |
| 41 | Lakeland_terrier | 0.003409 |
############################################################# Cas n°4
'Race indiquée par les données : Japanese_spaniel'
| Race prédite | Probabilité | |
|---|---|---|
| 51 | Pekinese | 0.682957 |
| 38 | Japanese_spaniel | 0.316960 |
| 63 | Shih-Tzu | 0.000057 |
############################################################# Cas n°5
'Race indiquée par les données : Yorkshire_terrier'
| Race prédite | Probabilité | |
|---|---|---|
| 111 | silky_terrier | 0.556030 |
| 73 | Yorkshire_terrier | 0.438188 |
| 5 | Australian_terrier | 0.005672 |
############################################################# Cas n°6
'Race indiquée par les données : Eskimo_dog'
| Race prédite | Probabilité | |
|---|---|---|
| 64 | Siberian_husky | 0.703306 |
| 24 | Eskimo_dog | 0.241922 |
| 101 | malamute | 0.054724 |
############################################################# Cas n°7
'Race indiquée par les données : Norwegian_elkhound'
| Race prédite | Probabilité | |
|---|---|---|
| 101 | malamute | 0.633134 |
| 48 | Norwegian_elkhound | 0.357203 |
| 24 | Eskimo_dog | 0.006938 |
############################################################# Cas n°8
'Race indiquée par les données : Dandie_Dinmont'
| Race prédite | Probabilité | |
|---|---|---|
| 68 | Tibetan_terrier | 0.726644 |
| 44 | Maltese_dog | 0.108286 |
| 43 | Lhasa | 0.054459 |
############################################################# Cas n°9
'Race indiquée par les données : Walker_hound'
| Race prédite | Probabilité | |
|---|---|---|
| 40 | Labrador_retriever | 0.055117 |
| 29 | Great_Dane | 0.046484 |
| 93 | flat-coated_retriever | 0.044625 |
############################################################# Cas n°10
'Race indiquée par les données : Greater_Swiss_Mountain_dog'
| Race prédite | Probabilité | |
|---|---|---|
| 23 | EntleBucher | 0.616895 |
| 31 | Greater_Swiss_Mountain_dog | 0.232895 |
| 4 | Appenzeller | 0.139398 |
############################################################# Cas n°11
'Race indiquée par les données : bluetick'
| Race prédite | Probabilité | |
|---|---|---|
| 27 | German_short-haired_pointer | 0.770263 |
| 80 | bluetick | 0.229263 |
| 40 | Labrador_retriever | 0.000402 |
############################################################# Cas n°12
'Race indiquée par les données : French_bulldog'
| Race prédite | Probabilité | |
|---|---|---|
| 17 | Chihuahua | 0.897360 |
| 25 | French_bulldog | 0.070206 |
| 103 | miniature_pinscher | 0.013219 |
############################################################# Cas n°13
'Race indiquée par les données : Cardigan'
| Race prédite | Probabilité | |
|---|---|---|
| 52 | Pembroke | 0.655918 |
| 15 | Cardigan | 0.344061 |
| 107 | papillon | 0.000019 |
############################################################# Cas n°14
'Race indiquée par les données : Rhodesian_ridgeback'
| Race prédite | Probabilité | |
|---|---|---|
| 103 | miniature_pinscher | 0.354982 |
| 116 | toy_terrier | 0.185895 |
| 13 | Brabancon_griffon | 0.129670 |
############################################################# Cas n°15
'Race indiquée par les données : Irish_wolfhound'
| Race prédite | Probabilité | |
|---|---|---|
| 60 | Scottish_deerhound | 9.515973e-01 |
| 36 | Irish_wolfhound | 4.840273e-02 |
| 118 | whippet | 6.883465e-10 |
############################################################# Cas n°16
'Race indiquée par les données : West_Highland_white_terrier'
| Race prédite | Probabilité | |
|---|---|---|
| 58 | Samoyed | 0.662487 |
| 72 | West_Highland_white_terrier | 0.152814 |
| 41 | Lakeland_terrier | 0.054210 |
############################################################# Cas n°17
'Race indiquée par les données : Irish_wolfhound'
| Race prédite | Probabilité | |
|---|---|---|
| 60 | Scottish_deerhound | 0.999046 |
| 36 | Irish_wolfhound | 0.000948 |
| 81 | borzoi | 0.000005 |
############################################################# Cas n°18
'Race indiquée par les données : Old_English_sheepdog'
| Race prédite | Probabilité | |
|---|---|---|
| 106 | otterhound | 0.450279 |
| 68 | Tibetan_terrier | 0.264938 |
| 50 | Old_English_sheepdog | 0.079104 |
############################################################# Cas n°19
'Race indiquée par les données : French_bulldog'
| Race prédite | Probabilité | |
|---|---|---|
| 13 | Brabancon_griffon | 0.614220 |
| 103 | miniature_pinscher | 0.207548 |
| 19 | Doberman | 0.033202 |
############################################################# Cas n°20
'Race indiquée par les données : American_Staffordshire_terrier'
| Race prédite | Probabilité | |
|---|---|---|
| 65 | Staffordshire_bullterrier | 0.576555 |
| 3 | American_Staffordshire_terrier | 0.423019 |
| 40 | Labrador_retriever | 0.000220 |
#############################################################
(Cas traité par l'association en fournissant leur propre jeu de donnée)
img = error_predict.uri[2]
display(load_img(img))
display("Race indiquée par les données : " + get_true_breed(img))
display(get_top3_breeds(model, img))
'Race indiquée par les données : Sealyham_terrier'
| Race prédite | Probabilité | |
|---|---|---|
| 112 | soft-coated_wheaten_terrier | 0.956069 |
| 61 | Sealyham_terrier | 0.037118 |
| 41 | Lakeland_terrier | 0.003409 |
test_file.uri[0]
'/content/drive/My Drive/Formation/Informatique - Digital/OpenClassroom/IML/P6_Classez_des_images_à_l_aide_d_algorithmes_de_deep_learning/dev/src/img/n02096585-Boston_bull/n02096585_1571.jpg'
file = path_img + 'src/img/n02095889-Sealyham_terrier/n02095889_61.jpg'
display(load_img(file))
file = path_img + 'src/img/n02098105-soft-coated_wheaten_terrier/n02098105_3591.jpg'
display(load_img(file))
Dans ce cas, il s'agit d'une erreur humaine. La photo est dans le mauvais dossier (mauvaise race attribué). Cependant, l'ordinateur a identifié la bonne race.
(Cas traité à l'aide du détecteur d'objet)
img = error_predict.uri[11]
display(load_img(img))
display("Race indiquée par les données : " + get_true_breed(img))
display(get_top3_breeds(model, img))
'Race indiquée par les données : French_bulldog'
| Race prédite | Probabilité | |
|---|---|---|
| 17 | Chihuahua | 0.897360 |
| 25 | French_bulldog | 0.070206 |
| 103 | miniature_pinscher | 0.013219 |
file = path_img + 'src/img/n02085620-Chihuahua/n02085620_952.jpg'
display(load_img(file))
En effet, il y a une erreur de prédiction. La race présentée n'est pas un Chihuahua. Renouvelons l'expérience avec la même photo, mais recadrée sur ce que l'on souhaite identifier.
img = path_img + 'src/img/img test api/_n02108915_623_recadrée.jpg'
display(load_img(img))
display(get_top3_breeds(model, img))
| Race prédite | Probabilité | |
|---|---|---|
| 25 | French_bulldog | 9.999774e-01 |
| 17 | Chihuahua | 2.222603e-05 |
| 15 | Cardigan | 4.017094e-07 |
Une fois la photo recadrée, la machine ne se trompe plus. Pour résoudre ce problème, nous pouvons former l'humain à mieux qualifier les photos à donner à la machine.
En corrigeant les erreurs vues dans les étapes précédentes, nous pouvons encore améliorer considérablement les performances de l'algorithme. Je recommande d'être soigneux dans le choix et l'identification des photos à donner en apprentissage pour la machine, mais aussi en production.
accuracy_best_model = round((test_file.shape[0] - error_predict.shape[0]) / test_file.shape[0]*100, 1)
print(f'Précision du modèle : {accuracy_best_model} %')
Précision du modèle : 87.4 %
class_report = classification_report(test_file.breeds, test_file.predicted_breeds, output_dict=True)
class_report_df = pd.DataFrame.from_dict(class_report, orient='columns').transpose().head(-3)
pd.set_option('display.max_rows', None)
class_report_df.sort_values('precision')
| precision | recall | f1-score | support | |
|---|---|---|---|---|
| Tibetan_mastiff | 0.509091 | 0.933333 | 0.658824 | 30.0 |
| Eskimo_dog | 0.538462 | 0.269231 | 0.358974 | 26.0 |
| English_foxhound | 0.568966 | 0.970588 | 0.717391 | 34.0 |
| miniature_poodle | 0.607143 | 0.680000 | 0.641509 | 25.0 |
| Siberian_husky | 0.623188 | 0.877551 | 0.728814 | 49.0 |
| Chesapeake_Bay_retriever | 0.659574 | 0.837838 | 0.738095 | 37.0 |
| collie | 0.666667 | 0.666667 | 0.666667 | 27.0 |
| Shih-Tzu | 0.675676 | 0.862069 | 0.757576 | 29.0 |
| Tibetan_terrier | 0.677419 | 0.933333 | 0.785047 | 45.0 |
| American_Staffordshire_terrier | 0.703704 | 0.612903 | 0.655172 | 31.0 |
| toy_poodle | 0.714286 | 0.588235 | 0.645161 | 17.0 |
| Rhodesian_ridgeback | 0.720000 | 0.750000 | 0.734694 | 24.0 |
| silky_terrier | 0.722222 | 0.906977 | 0.804124 | 43.0 |
| EntleBucher | 0.729167 | 1.000000 | 0.843373 | 35.0 |
| Cardigan | 0.744186 | 0.888889 | 0.810127 | 36.0 |
| Border_collie | 0.758621 | 0.846154 | 0.800000 | 26.0 |
| Lakeland_terrier | 0.767442 | 0.868421 | 0.814815 | 38.0 |
| Norwich_terrier | 0.775000 | 0.861111 | 0.815789 | 36.0 |
| Pekinese | 0.777778 | 0.903226 | 0.835821 | 31.0 |
| Doberman | 0.785714 | 0.880000 | 0.830189 | 25.0 |
| toy_terrier | 0.800000 | 0.914286 | 0.853333 | 35.0 |
| Scottish_deerhound | 0.800000 | 0.878049 | 0.837209 | 41.0 |
| Staffordshire_bullterrier | 0.800000 | 0.848485 | 0.823529 | 33.0 |
| cocker_spaniel | 0.800000 | 0.933333 | 0.861538 | 30.0 |
| Irish_terrier | 0.804878 | 0.970588 | 0.880000 | 34.0 |
| Chihuahua | 0.805556 | 0.906250 | 0.852941 | 32.0 |
| briard | 0.806452 | 0.925926 | 0.862069 | 27.0 |
| redbone | 0.814815 | 0.846154 | 0.830189 | 26.0 |
| Yorkshire_terrier | 0.823529 | 0.777778 | 0.800000 | 36.0 |
| malamute | 0.828571 | 0.743590 | 0.783784 | 39.0 |
| miniature_schnauzer | 0.828571 | 0.852941 | 0.840580 | 34.0 |
| bluetick | 0.840909 | 0.925000 | 0.880952 | 40.0 |
| Dandie_Dinmont | 0.846154 | 0.916667 | 0.880000 | 24.0 |
| Brittany_spaniel | 0.850000 | 0.894737 | 0.871795 | 19.0 |
| standard_schnauzer | 0.851852 | 0.766667 | 0.807018 | 30.0 |
| Airedale | 0.852941 | 0.906250 | 0.878788 | 32.0 |
| Shetland_sheepdog | 0.857143 | 0.818182 | 0.837209 | 22.0 |
| Great_Pyrenees | 0.859649 | 0.924528 | 0.890909 | 53.0 |
| Maltese_dog | 0.862745 | 0.936170 | 0.897959 | 47.0 |
| boxer | 0.866667 | 1.000000 | 0.928571 | 26.0 |
| dingo | 0.868421 | 0.942857 | 0.904110 | 35.0 |
| Italian_greyhound | 0.870968 | 0.771429 | 0.818182 | 35.0 |
| Australian_terrier | 0.875000 | 0.777778 | 0.823529 | 54.0 |
| Weimaraner | 0.880952 | 0.973684 | 0.925000 | 38.0 |
| Irish_water_spaniel | 0.882353 | 1.000000 | 0.937500 | 30.0 |
| Norfolk_terrier | 0.888889 | 0.800000 | 0.842105 | 40.0 |
| groenendael | 0.888889 | 1.000000 | 0.941176 | 32.0 |
| Sealyham_terrier | 0.888889 | 0.975610 | 0.930233 | 41.0 |
| vizsla | 0.888889 | 0.888889 | 0.888889 | 27.0 |
| Pembroke | 0.888889 | 0.750000 | 0.813559 | 32.0 |
| bloodhound | 0.894737 | 0.944444 | 0.918919 | 36.0 |
| Blenheim_spaniel | 0.894737 | 0.971429 | 0.931507 | 35.0 |
| English_setter | 0.894737 | 0.918919 | 0.906667 | 37.0 |
| flat-coated_retriever | 0.900000 | 0.964286 | 0.931034 | 28.0 |
| miniature_pinscher | 0.902439 | 0.804348 | 0.850575 | 46.0 |
| Welsh_springer_spaniel | 0.903226 | 1.000000 | 0.949153 | 28.0 |
| West_Highland_white_terrier | 0.903226 | 0.800000 | 0.848485 | 35.0 |
| Rottweiler | 0.906250 | 0.935484 | 0.920635 | 31.0 |
| otterhound | 0.906250 | 0.852941 | 0.878788 | 34.0 |
| bull_mastiff | 0.909091 | 0.937500 | 0.923077 | 32.0 |
| Appenzeller | 0.909091 | 0.689655 | 0.784314 | 29.0 |
| Bouvier_des_Flandres | 0.913043 | 0.875000 | 0.893617 | 24.0 |
| affenpinscher | 0.913043 | 0.954545 | 0.933333 | 22.0 |
| Irish_wolfhound | 0.913043 | 0.763636 | 0.831683 | 55.0 |
| Walker_hound | 0.916667 | 0.366667 | 0.523810 | 30.0 |
| Ibizan_hound | 0.918367 | 0.957447 | 0.937500 | 47.0 |
| German_short-haired_pointer | 0.920000 | 0.884615 | 0.901961 | 26.0 |
| golden_retriever | 0.921053 | 0.921053 | 0.921053 | 38.0 |
| black-and-tan_coonhound | 0.925000 | 0.948718 | 0.936709 | 39.0 |
| Labrador_retriever | 0.925000 | 0.880952 | 0.902439 | 42.0 |
| Saluki | 0.928571 | 1.000000 | 0.962963 | 39.0 |
| Border_terrier | 0.933333 | 1.000000 | 0.965517 | 28.0 |
| Saint_Bernard | 0.937500 | 0.937500 | 0.937500 | 32.0 |
| Lhasa | 0.937500 | 0.625000 | 0.750000 | 48.0 |
| German_shepherd | 0.937500 | 0.909091 | 0.923077 | 33.0 |
| borzoi | 0.939394 | 1.000000 | 0.968750 | 31.0 |
| beagle | 0.941176 | 0.695652 | 0.800000 | 46.0 |
| papillon | 0.942857 | 1.000000 | 0.970588 | 33.0 |
| soft-coated_wheaten_terrier | 0.945946 | 0.972222 | 0.958904 | 36.0 |
| malinois | 0.948718 | 0.973684 | 0.961039 | 38.0 |
| Leonberg | 0.948718 | 1.000000 | 0.973684 | 37.0 |
| giant_schnauzer | 0.953488 | 0.953488 | 0.953488 | 43.0 |
| Old_English_sheepdog | 0.954545 | 0.777778 | 0.857143 | 27.0 |
| Boston_bull | 0.954545 | 0.954545 | 0.954545 | 44.0 |
| schipperke | 0.958333 | 0.821429 | 0.884615 | 28.0 |
| chow | 0.958333 | 1.000000 | 0.978723 | 46.0 |
| Japanese_spaniel | 0.960000 | 0.774194 | 0.857143 | 31.0 |
| Newfoundland | 0.962963 | 0.553191 | 0.702703 | 47.0 |
| Brabancon_griffon | 0.964286 | 0.964286 | 0.964286 | 28.0 |
| Irish_setter | 0.965517 | 0.965517 | 0.965517 | 29.0 |
| Mexican_hairless | 0.967742 | 1.000000 | 0.983607 | 30.0 |
| Kerry_blue_terrier | 0.968750 | 0.939394 | 0.953846 | 33.0 |
| Greater_Swiss_Mountain_dog | 0.968750 | 0.861111 | 0.911765 | 36.0 |
| basenji | 0.971429 | 0.809524 | 0.883117 | 42.0 |
| Great_Dane | 0.972222 | 0.921053 | 0.945946 | 38.0 |
| Bedlington_terrier | 0.974359 | 0.974359 | 0.974359 | 39.0 |
| Bernese_mountain_dog | 0.975000 | 0.975000 | 0.975000 | 40.0 |
| Samoyed | 0.976190 | 0.953488 | 0.964706 | 43.0 |
| cairn | 0.977778 | 0.897959 | 0.936170 | 49.0 |
| pug | 1.000000 | 0.966667 | 0.983051 | 30.0 |
| standard_poodle | 1.000000 | 0.767442 | 0.868421 | 43.0 |
| Afghan_hound | 1.000000 | 0.970588 | 0.985075 | 34.0 |
| Scotch_terrier | 1.000000 | 0.906250 | 0.950820 | 32.0 |
| komondor | 1.000000 | 1.000000 | 1.000000 | 35.0 |
| African_hunting_dog | 1.000000 | 1.000000 | 1.000000 | 29.0 |
| English_springer | 1.000000 | 0.656250 | 0.792453 | 32.0 |
| French_bulldog | 1.000000 | 0.916667 | 0.956522 | 36.0 |
| Gordon_setter | 1.000000 | 1.000000 | 1.000000 | 24.0 |
| Norwegian_elkhound | 1.000000 | 0.897436 | 0.945946 | 39.0 |
| Pomeranian | 1.000000 | 0.900000 | 0.947368 | 40.0 |
| kuvasz | 1.000000 | 0.703704 | 0.826087 | 27.0 |
| whippet | 1.000000 | 0.742857 | 0.852459 | 35.0 |
| basset | 1.000000 | 0.937500 | 0.967742 | 32.0 |
| clumber | 1.000000 | 0.928571 | 0.962963 | 28.0 |
| curly-coated_retriever | 1.000000 | 0.777778 | 0.875000 | 27.0 |
| dhole | 1.000000 | 1.000000 | 1.000000 | 29.0 |
| keeshond | 1.000000 | 1.000000 | 1.000000 | 36.0 |
| kelpie | 1.000000 | 0.703704 | 0.826087 | 27.0 |
| Sussex_spaniel | 1.000000 | 0.921053 | 0.958904 | 38.0 |
| wire-haired_fox_terrier | 1.000000 | 0.666667 | 0.800000 | 27.0 |
%%capture
# clone darknet repo
!git clone https://github.com/AlexeyAB/darknet
# change makefile to have GPU and OPENCV enabled
%cd darknet
!sed -i 's/OPENCV=0/OPENCV=1/' Makefile
!sed -i 's/GPU=0/GPU=1/' Makefile
!sed -i 's/CUDNN=0/CUDNN=1/' Makefile
!sed -i 's/CUDNN_HALF=0/CUDNN_HALF=1/' Makefile
# verify CUDA
!/usr/local/cuda/bin/nvcc --version
# make darknet (builds darknet so that you can then use the darknet executable file to run or train object detectors)
!make
%%capture
!wget https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v3_optimal/yolov4.weights
error_predict.uri[1]
'/content/drive/My Drive/Formation/Informatique - Digital/OpenClassroom/IML/P6_Classez_des_images_à_l_aide_d_algorithmes_de_deep_learning/dev/src/img/n02097209-standard_schnauzer/n02097209_2382.jpg'
%%capture
!./darknet detector test cfg/coco.data cfg/yolov4.cfg yolov4.weights -ext_output "/content/drive/My Drive/Formation/Informatique - Digital/OpenClassroom/IML/P6_Classez_des_images_à_l_aide_d_algorithmes_de_deep_learning/dev/src/img/n02097209-standard_schnauzer/n02097209_2382.jpg"
load_img('predictions.jpg')
Le chien a bien été détecté. L'enjeu est maintenant d'isoler l'objet à classifier
%%capture
# Extraction des uri dans un fichier texte
error_predict.uri.to_csv('/content/images_error.txt', index=False, header=False)
# Détection des objets identifiés dans les images à partir du fichier texte
# Stockage des résultats dans un JSON
!./darknet detector test cfg/coco.data cfg/yolov4.cfg yolov4.weights -dont_show -ext_output -out /content/result.json < /content/images_error.txt
# Transformation du JSON en dataframe
result_crop = pd.read_json('/content/result.json')
# Récupération de l'objet identifié comme un chien avec la meilleur confidence
result_crop = get_best_object(result_crop)
# Assemblage des données dans un dataframe
error_predict_crop = pd.merge(error_predict, result_crop, left_index=True, right_index=True)
col = ['uri', 'breeds', 'predicted_breeds', 'center_x', 'center_y', 'width', 'height']
error_predict_crop = error_predict_crop[col]
error_predict_crop.head()
| uri | breeds | predicted_breeds | center_x | center_y | width | height | |
|---|---|---|---|---|---|---|---|
| 0 | /content/drive/My Drive/Formation/Informatique... | toy_poodle | miniature_poodle | 0.467455 | 0.507713 | 0.395568 | 0.650054 |
| 1 | /content/drive/My Drive/Formation/Informatique... | standard_schnauzer | miniature_schnauzer | 0.479141 | 0.612768 | 0.234255 | 0.769521 |
| 2 | /content/drive/My Drive/Formation/Informatique... | Sealyham_terrier | soft-coated_wheaten_terrier | 0.511100 | 0.512058 | 0.874051 | 0.911958 |
| 3 | /content/drive/My Drive/Formation/Informatique... | Japanese_spaniel | Pekinese | 0.559252 | 0.533149 | 0.713220 | 0.917417 |
| 4 | /content/drive/My Drive/Formation/Informatique... | Yorkshire_terrier | silky_terrier | 0.548168 | 0.513692 | 0.884070 | 0.869686 |
for i in range(20):
img = error_predict_crop.uri[i]
img = load_img(img)
# display(img)
width, height = img.size
# display(width, height)
if ~np.isnan(error_predict_crop.center_x[i]):
# Transformation des coordonnées relatives en coordonnées interprétables
center_x = width * error_predict_crop['center_x'][i]
center_y = height * error_predict_crop['center_y'][i]
h_crop = height * error_predict_crop['height'][i]
w_crop = width * error_predict_crop['width'][i]
left = center_x - w_crop / 2
top = center_y - h_crop / 2
right = center_x + w_crop / 2
bottom = center_y + h_crop / 2
# display(left, top, right, bottom)
# Rognage sur objet à identifier et transformation avant prédicition
img_cropped = img.crop((left, top, right, bottom))
display(img_cropped)
img_cropped = img_cropped.resize((299, 299))
input_array = np.reshape(img_to_array(img_cropped), (-1, 299, 299, 3)) / 255
# Prédiction de la race
output = model.predict(input_array)
df_out = pd.DataFrame(output[0])
top3_breeds = pd.merge(class_label, df_out, left_index=True, right_index=True).rename(columns={0: 'Probabilité', 'index': 'Race prédite'}).sort_values('Probabilité', ascending=False).head(3)
display(top3_breeds)
else: print("Non identifié comme un chien")
| Race prédite | Probabilité | |
|---|---|---|
| 104 | miniature_poodle | 0.949365 |
| 115 | toy_poodle | 0.050607 |
| 41 | Lakeland_terrier | 0.000025 |
| Race prédite | Probabilité | |
|---|---|---|
| 114 | standard_schnauzer | 0.787192 |
| 105 | miniature_schnauzer | 0.212792 |
| 94 | giant_schnauzer | 0.000015 |
| Race prédite | Probabilité | |
|---|---|---|
| 112 | soft-coated_wheaten_terrier | 0.600788 |
| 61 | Sealyham_terrier | 0.371660 |
| 39 | Kerry_blue_terrier | 0.017642 |
| Race prédite | Probabilité | |
|---|---|---|
| 38 | Japanese_spaniel | 0.831609 |
| 51 | Pekinese | 0.168314 |
| 63 | Shih-Tzu | 0.000053 |
| Race prédite | Probabilité | |
|---|---|---|
| 111 | silky_terrier | 0.569506 |
| 73 | Yorkshire_terrier | 0.427583 |
| 5 | Australian_terrier | 0.002841 |
| Race prédite | Probabilité | |
|---|---|---|
| 64 | Siberian_husky | 0.801461 |
| 24 | Eskimo_dog | 0.181698 |
| 101 | malamute | 0.016807 |
| Race prédite | Probabilité | |
|---|---|---|
| 48 | Norwegian_elkhound | 0.998361 |
| 101 | malamute | 0.001198 |
| 97 | keeshond | 0.000360 |
| Race prédite | Probabilité | |
|---|---|---|
| 68 | Tibetan_terrier | 0.823656 |
| 44 | Maltese_dog | 0.061282 |
| 38 | Japanese_spaniel | 0.042293 |
Non identifié comme un chien
| Race prédite | Probabilité | |
|---|---|---|
| 31 | Greater_Swiss_Mountain_dog | 0.985689 |
| 4 | Appenzeller | 0.010193 |
| 23 | EntleBucher | 0.003996 |
| Race prédite | Probabilité | |
|---|---|---|
| 80 | bluetick | 0.977133 |
| 27 | German_short-haired_pointer | 0.022844 |
| 40 | Labrador_retriever | 0.000016 |
| Race prédite | Probabilité | |
|---|---|---|
| 25 | French_bulldog | 0.993133 |
| 17 | Chihuahua | 0.006508 |
| 116 | toy_terrier | 0.000185 |
| Race prédite | Probabilité | |
|---|---|---|
| 15 | Cardigan | 0.708011 |
| 52 | Pembroke | 0.291971 |
| 107 | papillon | 0.000014 |
| Race prédite | Probabilité | |
|---|---|---|
| 54 | Rhodesian_ridgeback | 0.605623 |
| 103 | miniature_pinscher | 0.335818 |
| 116 | toy_terrier | 0.029752 |
| Race prédite | Probabilité | |
|---|---|---|
| 60 | Scottish_deerhound | 9.974892e-01 |
| 36 | Irish_wolfhound | 2.510746e-03 |
| 81 | borzoi | 1.899020e-08 |
| Race prédite | Probabilité | |
|---|---|---|
| 72 | West_Highland_white_terrier | 0.921482 |
| 61 | Sealyham_terrier | 0.065799 |
| 41 | Lakeland_terrier | 0.006935 |
| Race prédite | Probabilité | |
|---|---|---|
| 60 | Scottish_deerhound | 0.999222 |
| 36 | Irish_wolfhound | 0.000768 |
| 81 | borzoi | 0.000010 |
| Race prédite | Probabilité | |
|---|---|---|
| 106 | otterhound | 0.564522 |
| 36 | Irish_wolfhound | 0.077975 |
| 68 | Tibetan_terrier | 0.056784 |
| Race prédite | Probabilité | |
|---|---|---|
| 25 | French_bulldog | 0.999995 |
| 11 | Boston_bull | 0.000002 |
| 108 | pug | 0.000002 |
| Race prédite | Probabilité | |
|---|---|---|
| 65 | Staffordshire_bullterrier | 0.718867 |
| 3 | American_Staffordshire_terrier | 0.281021 |
| 40 | Labrador_retriever | 0.000040 |
Les chiens sont effectivement bien isolés, et les races sont putôt bien prédites. Hormis la gestion des erreurs humaine de classification de races.
# TO DO : Evaluer la réduction d'erreur uniquement sur le fichier d'erreur
error_predict_crop['predicted_breeds_crop'] = np.nan
for i in range(len(error_predict_crop)):
# print(f'Traitement de la photo {i} en cours ...')
img = error_predict_crop.uri[i]
img = load_img(img)
width, height = img.size
# Rognage si chien détecté
if ~np.isnan(error_predict_crop.center_x[i]):
# Transformation des coordonnées relatives en coordonnées interprétables
center_x = width * error_predict_crop['center_x'][i]
center_y = height * error_predict_crop['center_y'][i]
h_crop = height * error_predict_crop['height'][i]
w_crop = width * error_predict_crop['width'][i]
left = center_x - w_crop / 2
top = center_y - h_crop / 2
right = center_x + w_crop / 2
bottom = center_y + h_crop / 2
# Rognage sur objet à identifier et transformation avant prédicition
img = img.crop((left, top, right, bottom))
# Classification de la photo avec ou sans rognage
img = img.resize((299, 299))
input_array = np.reshape(img_to_array(img), (-1, 299, 299, 3)) / 255
# Prédiction de la race
output = model.predict(input_array)
breeds = class_label.at[output.argmax(axis=-1)[0], 'index']
error_predict_crop['predicted_breeds_crop'].loc[i] = breeds
/usr/local/lib/python3.6/dist-packages/pandas/core/indexing.py:670: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy iloc._setitem_with_indexer(indexer, value)
n_new_error = error_predict_crop[error_predict_crop.breeds != error_predict_crop.predicted_breeds_crop].shape[0]
error_reduce = round(1 - n_new_error / error_predict_crop.shape[0], 2) * 100
print(f"Les erreurs de prédiction ont été réduites de {error_reduce} % sur le fichier d'erreur.")
Les erreurs de prédiction ont été réduites de 35.0 % sur le fichier d'erreur.
L'implémentation du l'algorithmes peut induire des erreurs qui n'aurait pas eu pas lieu. Cherchons à les quantifier.
%%capture
# Extraction des uri dans un fichier texte
test_file.uri.to_csv('/content/images_test.txt', index=False, header=False)
# Détection des objets identifiés dans les images à partir du fichier texte
# Stockage des résultats dans un JSON
!./darknet detector test cfg/coco.data cfg/yolov4.cfg yolov4.weights -dont_show -ext_output -out "/content/drive/My Drive/Formation/Informatique - Digital/OpenClassroom/IML/P7_Développez_une_preuve_de_concept/dev/src/result_test.json" < /content/images_test.txt
# Transformation du JSON en dataframe
result_crop = pd.read_json("/content/drive/My Drive/Formation/Informatique - Digital/OpenClassroom/IML/P7_Développez_une_preuve_de_concept/dev/src/result_test.json")
# Récupération de l'objet identifié comme un chien avec la meilleur confidence
result_crop = get_best_object(result_crop)
# Assemblage des données dans un dataframe
test_file = pd.merge(test_file, result_crop, left_index=True, right_index=True)
col = ['uri', 'breeds', 'predicted_breeds', 'center_x', 'center_y', 'width', 'height']
test_file = test_file[col]
test_file.head()
| uri | breeds | predicted_breeds | center_x | center_y | width | height | |
|---|---|---|---|---|---|---|---|
| 0 | /content/drive/My Drive/Formation/Informatique... | Boston_bull | Boston_bull | 0.506313 | 0.497488 | 0.705340 | 0.855740 |
| 1 | /content/drive/My Drive/Formation/Informatique... | toy_poodle | miniature_poodle | 0.467455 | 0.507713 | 0.395568 | 0.650054 |
| 2 | /content/drive/My Drive/Formation/Informatique... | Staffordshire_bullterrier | Staffordshire_bullterrier | 0.549928 | 0.623763 | 0.929981 | 0.762959 |
| 3 | /content/drive/My Drive/Formation/Informatique... | dingo | dingo | 0.443700 | 0.504583 | 0.822173 | 0.989210 |
| 4 | /content/drive/My Drive/Formation/Informatique... | West_Highland_white_terrier | West_Highland_white_terrier | 0.548143 | 0.547041 | 0.432979 | 0.712642 |
test_file['predicted_breeds_crop'] = np.nan
for i in range(len(test_file)):
# print(f'Traitement de la photo {i} en cours ...')
img = test_file.uri[i]
img = load_img(img)
width, height = img.size
# Rognage si chien détecté
if ~np.isnan(test_file.center_x[i]):
# Transformation des coordonnées relatives en coordonnées interprétables
center_x = width * test_file['center_x'][i]
center_y = height * test_file['center_y'][i]
h_crop = height * test_file['height'][i]
w_crop = width * test_file['width'][i]
left = center_x - w_crop / 2
top = center_y - h_crop / 2
right = center_x + w_crop / 2
bottom = center_y + h_crop / 2
# Rognage sur objet à identifier et transformation avant prédicition
img = img.crop((left, top, right, bottom))
# Classification de la photo avec ou sans rognage
img = img.resize((299, 299))
input_array = np.reshape(img_to_array(img), (-1, 299, 299, 3)) / 255
# Prédiction de la race
output = model.predict(input_array)
breeds = class_label.at[output.argmax(axis=-1)[0], 'index']
test_file['predicted_breeds_crop'].loc[i] = breeds
/usr/local/lib/python3.6/dist-packages/pandas/core/indexing.py:670: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy iloc._setitem_with_indexer(indexer, value)
test_error_predict_crop = test_file[test_file.breeds != test_file.predicted_breeds_crop]
test_error_predict_crop.reset_index(drop=True, inplace=True)
accuracy_best_model = round((test_file.shape[0] - test_error_predict_crop.shape[0]) / test_file.shape[0]*100, 1)
print(f'Précision du modèle : {accuracy_best_model} %')
Précision du modèle : 87.5 %
La précision de modèle n'a quasiment pas évolué (+ 0.1 %). Je vais comprendre les erreurs induites
for i in range(20):
print(f"Cas n°{i}")
img = test_error_predict_crop.uri[i]
img = load_img(img)
display(img)
width, height = img.size
# Rognage si chien détecté
if ~np.isnan(test_error_predict_crop.center_x[i]):
# Transformation des coordonnées relatives en coordonnées interprétables
center_x = width * test_error_predict_crop['center_x'][i]
center_y = height * test_error_predict_crop['center_y'][i]
h_crop = height * test_error_predict_crop['height'][i]
w_crop = width * test_error_predict_crop['width'][i]
left = center_x - w_crop / 2
top = center_y - h_crop / 2
right = center_x + w_crop / 2
bottom = center_y + h_crop / 2
# Rognage sur objet à identifier et transformation avant prédicition
img = img.crop((left, top, right, bottom))
display(img)
# Classification de la photo avec ou sans rognage
img = img.resize((299, 299))
input_array = np.reshape(img_to_array(img), (-1, 299, 299, 3)) / 255
# Prédiction de la race
output = model.predict(input_array)
breeds = class_label.at[output.argmax(axis=-1)[0], 'index']
print(f'Race donnée : {test_error_predict_crop.breeds[i]}')
print(f'Race sans rognage : {test_error_predict_crop.predicted_breeds[i]}')
print(f'Race prédite avec rognage si détection : {breeds}')
print("#############################################################")
print("")
Cas n°0
Race donnée : toy_poodle Race sans rognage : miniature_poodle Race prédite avec rognage si détection : miniature_poodle ############################################################# Cas n°1
Race donnée : cocker_spaniel Race sans rognage : cocker_spaniel Race prédite avec rognage si détection : Blenheim_spaniel ############################################################# Cas n°2
Race donnée : West_Highland_white_terrier Race sans rognage : West_Highland_white_terrier Race prédite avec rognage si détection : Sealyham_terrier ############################################################# Cas n°3
Race donnée : Sealyham_terrier Race sans rognage : soft-coated_wheaten_terrier Race prédite avec rognage si détection : soft-coated_wheaten_terrier ############################################################# Cas n°4
Race donnée : Irish_wolfhound Race sans rognage : Irish_wolfhound Race prédite avec rognage si détection : cairn ############################################################# Cas n°5
Race donnée : Yorkshire_terrier Race sans rognage : silky_terrier Race prédite avec rognage si détection : silky_terrier ############################################################# Cas n°6
Race donnée : Eskimo_dog Race sans rognage : Siberian_husky Race prédite avec rognage si détection : Siberian_husky ############################################################# Cas n°7
Race donnée : Dandie_Dinmont Race sans rognage : Tibetan_terrier Race prédite avec rognage si détection : Tibetan_terrier ############################################################# Cas n°8
Race donnée : Walker_hound Race sans rognage : Labrador_retriever Race prédite avec rognage si détection : Labrador_retriever ############################################################# Cas n°9
Race donnée : chow Race sans rognage : chow Race prédite avec rognage si détection : Tibetan_terrier ############################################################# Cas n°10
Race donnée : Newfoundland Race sans rognage : Newfoundland Race prédite avec rognage si détection : Leonberg ############################################################# Cas n°11
Race donnée : Irish_wolfhound Race sans rognage : Scottish_deerhound Race prédite avec rognage si détection : Scottish_deerhound ############################################################# Cas n°12
Race donnée : Labrador_retriever Race sans rognage : Labrador_retriever Race prédite avec rognage si détection : Chesapeake_Bay_retriever ############################################################# Cas n°13
Race donnée : Samoyed Race sans rognage : Samoyed Race prédite avec rognage si détection : Great_Pyrenees ############################################################# Cas n°14
Race donnée : Irish_wolfhound Race sans rognage : Scottish_deerhound Race prédite avec rognage si détection : Scottish_deerhound ############################################################# Cas n°15
Race donnée : Old_English_sheepdog Race sans rognage : otterhound Race prédite avec rognage si détection : otterhound ############################################################# Cas n°16
Race donnée : Labrador_retriever Race sans rognage : Labrador_retriever Race prédite avec rognage si détection : Great_Dane ############################################################# Cas n°17
Race donnée : American_Staffordshire_terrier Race sans rognage : Staffordshire_bullterrier Race prédite avec rognage si détection : Staffordshire_bullterrier ############################################################# Cas n°18
Race donnée : Walker_hound Race sans rognage : EntleBucher Race prédite avec rognage si détection : EntleBucher ############################################################# Cas n°19
Race donnée : Eskimo_dog Race sans rognage : Siberian_husky Race prédite avec rognage si détection : Siberian_husky #############################################################
test_error_predict_crop.uri[4]
'/content/drive/My Drive/Formation/Informatique - Digital/OpenClassroom/IML/P6_Classez_des_images_à_l_aide_d_algorithmes_de_deep_learning/dev/src/img/n02090721-Irish_wolfhound/n02090721_1954.jpg'
%%capture
!./darknet detector test cfg/coco.data cfg/yolov4.cfg yolov4.weights -ext_output "/content/drive/My Drive/Formation/Informatique - Digital/OpenClassroom/IML/P6_Classez_des_images_à_l_aide_d_algorithmes_de_deep_learning/dev/src/img/n02090721-Irish_wolfhound/n02090721_1954.jpg"
load_img('predictions.jpg')
Dans ce cas, le chien principal est identifié comme un cheval, il est naturellement exclu de la zone d'analyse par l'algorithme. La machine identifie donc le chien en fond d'image.
test_error_predict_crop.uri[8]
'/content/drive/My Drive/Formation/Informatique - Digital/OpenClassroom/IML/P6_Classez_des_images_à_l_aide_d_algorithmes_de_deep_learning/dev/src/img/n02089867-Walker_hound/n02089867_2387.jpg'
%%capture
!./darknet detector test cfg/coco.data cfg/yolov4.cfg yolov4.weights -ext_output "/content/drive/My Drive/Formation/Informatique - Digital/OpenClassroom/IML/P6_Classez_des_images_à_l_aide_d_algorithmes_de_deep_learning/dev/src/img/n02089867-Walker_hound/n02089867_2387.jpg"
load_img('predictions.jpg')
Certainement du fait du grillage, l'animal est identifié comme un mouton. L'image est envoyé d'origine au classifieur. Dans ce cas précis et dans un autre étape, nous pourrions être tenter de reconstruire l'image.
test_error_predict_crop.uri[9]
'/content/drive/My Drive/Formation/Informatique - Digital/OpenClassroom/IML/P6_Classez_des_images_à_l_aide_d_algorithmes_de_deep_learning/dev/src/img/n02112137-chow/n02112137_12741.jpg'
%%capture
!./darknet detector test cfg/coco.data cfg/yolov4.cfg yolov4.weights -ext_output "/content/drive/My Drive/Formation/Informatique - Digital/OpenClassroom/IML/P6_Classez_des_images_à_l_aide_d_algorithmes_de_deep_learning/dev/src/img/n02112137-chow/n02112137_12741.jpg"
load_img('predictions.jpg')
Ici, le chien prenant le plus de place sur la photos et reconnu comme race par les données, n'est pas reconnue comme chien principal envoyé à l'algorithme.
class_report = classification_report(test_file.breeds, test_file.predicted_breeds_crop, output_dict=True)
class_report_df = pd.DataFrame.from_dict(class_report, orient='columns').transpose().head(-3)
pd.set_option('display.max_rows', None)
class_report_df.sort_values('precision')
| precision | recall | f1-score | support | |
|---|---|---|---|---|
| miniature_poodle | 0.529412 | 0.720000 | 0.610169 | 25.0 |
| English_foxhound | 0.542373 | 0.941176 | 0.688172 | 34.0 |
| Tibetan_mastiff | 0.562500 | 0.900000 | 0.692308 | 30.0 |
| Siberian_husky | 0.589041 | 0.877551 | 0.704918 | 49.0 |
| Eskimo_dog | 0.636364 | 0.269231 | 0.378378 | 26.0 |
| toy_terrier | 0.647059 | 0.942857 | 0.767442 | 35.0 |
| Border_collie | 0.648649 | 0.923077 | 0.761905 | 26.0 |
| Cardigan | 0.680851 | 0.888889 | 0.771084 | 36.0 |
| Tibetan_terrier | 0.704918 | 0.955556 | 0.811321 | 45.0 |
| Yorkshire_terrier | 0.717949 | 0.777778 | 0.746667 | 36.0 |
| American_Staffordshire_terrier | 0.718750 | 0.741935 | 0.730159 | 31.0 |
| Walker_hound | 0.722222 | 0.433333 | 0.541667 | 30.0 |
| EntleBucher | 0.744681 | 1.000000 | 0.853659 | 35.0 |
| toy_poodle | 0.750000 | 0.352941 | 0.480000 | 17.0 |
| silky_terrier | 0.750000 | 0.767442 | 0.758621 | 43.0 |
| Shih-Tzu | 0.781250 | 0.862069 | 0.819672 | 29.0 |
| Brittany_spaniel | 0.782609 | 0.947368 | 0.857143 | 19.0 |
| cocker_spaniel | 0.783784 | 0.966667 | 0.865672 | 30.0 |
| Dandie_Dinmont | 0.785714 | 0.916667 | 0.846154 | 24.0 |
| Great_Pyrenees | 0.786885 | 0.905660 | 0.842105 | 53.0 |
| Pembroke | 0.791667 | 0.593750 | 0.678571 | 32.0 |
| Staffordshire_bullterrier | 0.794118 | 0.818182 | 0.805970 | 33.0 |
| Chesapeake_Bay_retriever | 0.804878 | 0.891892 | 0.846154 | 37.0 |
| miniature_schnauzer | 0.804878 | 0.970588 | 0.880000 | 34.0 |
| Pekinese | 0.805556 | 0.935484 | 0.865672 | 31.0 |
| Doberman | 0.814815 | 0.880000 | 0.846154 | 25.0 |
| German_shepherd | 0.815789 | 0.939394 | 0.873239 | 33.0 |
| malamute | 0.823529 | 0.717949 | 0.767123 | 39.0 |
| briard | 0.827586 | 0.888889 | 0.857143 | 27.0 |
| Welsh_springer_spaniel | 0.833333 | 0.892857 | 0.862069 | 28.0 |
| Scottish_deerhound | 0.833333 | 0.975610 | 0.898876 | 41.0 |
| Lakeland_terrier | 0.833333 | 0.921053 | 0.875000 | 38.0 |
| Italian_greyhound | 0.837838 | 0.885714 | 0.861111 | 35.0 |
| vizsla | 0.838710 | 0.962963 | 0.896552 | 27.0 |
| West_Highland_white_terrier | 0.848485 | 0.800000 | 0.823529 | 35.0 |
| Shetland_sheepdog | 0.850000 | 0.772727 | 0.809524 | 22.0 |
| Sealyham_terrier | 0.851064 | 0.975610 | 0.909091 | 41.0 |
| bluetick | 0.857143 | 0.900000 | 0.878049 | 40.0 |
| Norfolk_terrier | 0.857143 | 0.900000 | 0.878049 | 40.0 |
| dingo | 0.864865 | 0.914286 | 0.888889 | 35.0 |
| Blenheim_spaniel | 0.871795 | 0.971429 | 0.918919 | 35.0 |
| Labrador_retriever | 0.878049 | 0.857143 | 0.867470 | 42.0 |
| Irish_water_spaniel | 0.878788 | 0.966667 | 0.920635 | 30.0 |
| German_short-haired_pointer | 0.880000 | 0.846154 | 0.862745 | 26.0 |
| Rottweiler | 0.882353 | 0.967742 | 0.923077 | 31.0 |
| Airedale | 0.885714 | 0.968750 | 0.925373 | 32.0 |
| Norwich_terrier | 0.888889 | 0.888889 | 0.888889 | 36.0 |
| Lhasa | 0.888889 | 0.666667 | 0.761905 | 48.0 |
| Great_Dane | 0.894737 | 0.894737 | 0.894737 | 38.0 |
| Japanese_spaniel | 0.896552 | 0.838710 | 0.866667 | 31.0 |
| golden_retriever | 0.897436 | 0.921053 | 0.909091 | 38.0 |
| Australian_terrier | 0.900000 | 0.833333 | 0.865385 | 54.0 |
| black-and-tan_coonhound | 0.902439 | 0.948718 | 0.925000 | 39.0 |
| Border_terrier | 0.903226 | 1.000000 | 0.949153 | 28.0 |
| otterhound | 0.906250 | 0.852941 | 0.878788 | 34.0 |
| Chihuahua | 0.906250 | 0.906250 | 0.906250 | 32.0 |
| dhole | 0.906250 | 1.000000 | 0.950820 | 29.0 |
| Bouvier_des_Flandres | 0.909091 | 0.833333 | 0.869565 | 24.0 |
| groenendael | 0.914286 | 1.000000 | 0.955224 | 32.0 |
| standard_schnauzer | 0.916667 | 0.733333 | 0.814815 | 30.0 |
| Irish_terrier | 0.918919 | 1.000000 | 0.957746 | 34.0 |
| bloodhound | 0.921053 | 0.972222 | 0.945946 | 36.0 |
| English_setter | 0.921053 | 0.945946 | 0.933333 | 37.0 |
| Weimaraner | 0.923077 | 0.947368 | 0.935065 | 38.0 |
| boxer | 0.925926 | 0.961538 | 0.943396 | 26.0 |
| Saluki | 0.926829 | 0.974359 | 0.950000 | 39.0 |
| Samoyed | 0.928571 | 0.906977 | 0.917647 | 43.0 |
| Brabancon_griffon | 0.931034 | 0.964286 | 0.947368 | 28.0 |
| pug | 0.931034 | 0.900000 | 0.915254 | 30.0 |
| Ibizan_hound | 0.937500 | 0.957447 | 0.947368 | 47.0 |
| collie | 0.941176 | 0.592593 | 0.727273 | 27.0 |
| papillon | 0.942857 | 1.000000 | 0.970588 | 33.0 |
| Leonberg | 0.947368 | 0.972973 | 0.960000 | 37.0 |
| kuvasz | 0.950000 | 0.703704 | 0.808511 | 27.0 |
| miniature_pinscher | 0.950000 | 0.826087 | 0.883721 | 46.0 |
| Bedlington_terrier | 0.951220 | 1.000000 | 0.975000 | 39.0 |
| Bernese_mountain_dog | 0.951220 | 0.975000 | 0.962963 | 40.0 |
| giant_schnauzer | 0.951220 | 0.906977 | 0.928571 | 43.0 |
| Irish_wolfhound | 0.953488 | 0.745455 | 0.836735 | 55.0 |
| Boston_bull | 0.954545 | 0.954545 | 0.954545 | 44.0 |
| Rhodesian_ridgeback | 0.954545 | 0.875000 | 0.913043 | 24.0 |
| kelpie | 0.954545 | 0.777778 | 0.857143 | 27.0 |
| curly-coated_retriever | 0.954545 | 0.777778 | 0.857143 | 27.0 |
| affenpinscher | 0.956522 | 1.000000 | 0.977778 | 22.0 |
| Maltese_dog | 0.957447 | 0.957447 | 0.957447 | 47.0 |
| chow | 0.957447 | 0.978261 | 0.967742 | 46.0 |
| Gordon_setter | 0.960000 | 1.000000 | 0.979592 | 24.0 |
| redbone | 0.960000 | 0.923077 | 0.941176 | 26.0 |
| flat-coated_retriever | 0.962963 | 0.928571 | 0.945455 | 28.0 |
| schipperke | 0.962963 | 0.928571 | 0.945455 | 28.0 |
| Scotch_terrier | 0.965517 | 0.875000 | 0.918033 | 32.0 |
| borzoi | 0.966667 | 0.935484 | 0.950820 | 31.0 |
| Saint_Bernard | 0.966667 | 0.906250 | 0.935484 | 32.0 |
| Mexican_hairless | 0.967742 | 1.000000 | 0.983607 | 30.0 |
| bull_mastiff | 0.967742 | 0.937500 | 0.952381 | 32.0 |
| basset | 0.967742 | 0.937500 | 0.952381 | 32.0 |
| Kerry_blue_terrier | 0.968750 | 0.939394 | 0.953846 | 33.0 |
| Greater_Swiss_Mountain_dog | 0.968750 | 0.861111 | 0.911765 | 36.0 |
| standard_poodle | 0.969697 | 0.744186 | 0.842105 | 43.0 |
| soft-coated_wheaten_terrier | 0.971429 | 0.944444 | 0.957746 | 36.0 |
| malinois | 0.971429 | 0.894737 | 0.931507 | 38.0 |
| French_bulldog | 0.972222 | 0.972222 | 0.972222 | 36.0 |
| Pomeranian | 0.972222 | 0.875000 | 0.921053 | 40.0 |
| cairn | 0.977778 | 0.897959 | 0.936170 | 49.0 |
| Afghan_hound | 1.000000 | 0.970588 | 0.985075 | 34.0 |
| keeshond | 1.000000 | 1.000000 | 1.000000 | 36.0 |
| clumber | 1.000000 | 0.928571 | 0.962963 | 28.0 |
| beagle | 1.000000 | 0.586957 | 0.739726 | 46.0 |
| basenji | 1.000000 | 0.880952 | 0.936709 | 42.0 |
| Sussex_spaniel | 1.000000 | 0.947368 | 0.972973 | 38.0 |
| whippet | 1.000000 | 0.657143 | 0.793103 | 35.0 |
| Old_English_sheepdog | 1.000000 | 0.851852 | 0.920000 | 27.0 |
| Norwegian_elkhound | 1.000000 | 0.923077 | 0.960000 | 39.0 |
| Newfoundland | 1.000000 | 0.489362 | 0.657143 | 47.0 |
| Irish_setter | 1.000000 | 0.931034 | 0.964286 | 29.0 |
| English_springer | 1.000000 | 0.593750 | 0.745098 | 32.0 |
| Appenzeller | 1.000000 | 0.724138 | 0.840000 | 29.0 |
| African_hunting_dog | 1.000000 | 1.000000 | 1.000000 | 29.0 |
| komondor | 1.000000 | 0.971429 | 0.985507 | 35.0 |
| wire-haired_fox_terrier | 1.000000 | 0.666667 | 0.800000 | 27.0 |
dogs = pd.read_csv(path_img + 'src/dogs.csv')
dogs.uri = dogs.uri.apply(lambda x : path_img + x).str.replace('\\', '/')
dogs['breeds'] = dogs.uri.str.split('/').str.get(12).str.slice(10)
dogs['filename'] = dogs['uri'].str.split('/').str.get(13)
dogs
| uri | breeds | filename | |
|---|---|---|---|
| 0 | /content/drive/My Drive/Formation/Informatique... | Chihuahua | n02085620_10074.jpg |
| 1 | /content/drive/My Drive/Formation/Informatique... | Chihuahua | n02085620_10131.jpg |
| 2 | /content/drive/My Drive/Formation/Informatique... | Chihuahua | n02085620_10621.jpg |
| 3 | /content/drive/My Drive/Formation/Informatique... | Chihuahua | n02085620_1073.jpg |
| 4 | /content/drive/My Drive/Formation/Informatique... | Chihuahua | n02085620_10976.jpg |
| ... | ... | ... | ... |
| 20574 | /content/drive/My Drive/Formation/Informatique... | African_hunting_dog | n02116738_9798.jpg |
| 20575 | /content/drive/My Drive/Formation/Informatique... | African_hunting_dog | n02116738_9818.jpg |
| 20576 | /content/drive/My Drive/Formation/Informatique... | African_hunting_dog | n02116738_9829.jpg |
| 20577 | /content/drive/My Drive/Formation/Informatique... | African_hunting_dog | n02116738_9844.jpg |
| 20578 | /content/drive/My Drive/Formation/Informatique... | African_hunting_dog | n02116738_9924.jpg |
20579 rows × 3 columns
%%capture
# Extraction des uri dans un fichier texte
dogs.uri.to_csv('/content/images_dogs.txt', index=False, header=False)
# Détection des objets identifiés dans les images à partir du fichier texte
# Stockage des résultats dans un JSON
!./darknet detector test cfg/coco.data cfg/yolov4.cfg yolov4.weights -dont_show -ext_output -out /content/result.json < /content/images_dogs.txt
# Transformation du JSON en dataframe
result_crop = pd.read_json('/content/result.json')
# Récupération de l'objet identifié comme un chien avec la meilleur confidence
result_crop = get_best_object(result_crop)
# Assemblage des données dans un dataframe
dogs = pd.merge(dogs, result_crop, left_index=True, right_index=True)
col = ['uri', 'breeds', 'filename', 'center_x', 'center_y', 'width', 'height']
dogs = dogs[col]
dogs.head()
| uri | breeds | filename | center_x | center_y | width | height | |
|---|---|---|---|---|---|---|---|
| 0 | /content/drive/My Drive/Formation/Informatique... | Chihuahua | n02085620_10074.jpg | 0.455568 | 0.489279 | 0.804954 | 1.057105 |
| 1 | /content/drive/My Drive/Formation/Informatique... | Chihuahua | n02085620_10131.jpg | 0.548339 | 0.504275 | 0.855886 | 0.966300 |
| 2 | /content/drive/My Drive/Formation/Informatique... | Chihuahua | n02085620_10621.jpg | 0.482755 | 0.502806 | 0.389775 | 0.703651 |
| 3 | /content/drive/My Drive/Formation/Informatique... | Chihuahua | n02085620_1073.jpg | 0.488581 | 0.530515 | 0.992405 | 0.990712 |
| 4 | /content/drive/My Drive/Formation/Informatique... | Chihuahua | n02085620_10976.jpg | 0.517973 | 0.559173 | 0.490166 | 0.733000 |
for i in range(len(dogs)):
# print(f'Traitement de la photo {i} en cours ...')
img = dogs.uri[i]
img = load_img(img)
width, height = img.size
# Rognage si chien détecté
if ~np.isnan(dogs.center_x[i]):
# Transformation des coordonnées relatives en coordonnées interprétables
center_x = width * dogs['center_x'][i]
center_y = height * dogs['center_y'][i]
h_crop = height * dogs['height'][i]
w_crop = width * dogs['width'][i]
left = center_x - w_crop / 2
top = center_y - h_crop / 2
right = center_x + w_crop / 2
bottom = center_y + h_crop / 2
# Rognage sur objet à identifier et transformation avant prédicition
img = img.crop((left, top, right, bottom))
# Enregistrement de la photos
path_crop = path + 'img_crop/' + dogs.breeds[i] + '/'
img.save(path_crop + dogs.filename[i])